using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using System.Text;
using System.Collections;
using System.Web.UI.HtmlControls;
using System.Globalization;
using System.Web.Script.Services;
using System.Web.Services;
using System.Xml;
using ResourceBlender.DAL;

/// <summary>
/// A page which shows graphs detailing the completion of resources assigned to an application
/// </summary>
public partial class ResourceSummary : LocalPage
{
    /// <summary>
    /// Displays available applications
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void Page_Load(object sender, EventArgs e)
    {
        pnlError.Visible = false;
        lblNothingSelected.Visible = false;
        if(!Page.IsPostBack) {
            litAddAppLink.Text = Utils.FormatLink(Resources.UIStrings.AddAppLink, ResolveUrl("~/Applications.aspx"));
            chlApplication.DataBind();
            if(chlApplication.Items.Count > 0) {
                mltMain.ActiveViewIndex = 1;
            }
        }
    }
        
    /// <summary>
    /// Generates, then displays a resource utilization summary.
    /// </summary>
    /// <param name="sender">The source of the event.</param>
    /// <param name="e">The <see cref="System.EventArgs"/> instance containing the event data.</param>
    protected void btnShowSummary_Click(object sender, EventArgs e) {
        if(chlApplication.SelectedIndex < 0) {
            lblNothingSelected.Visible = true;
            return;
        }
        List<string> selectedApps = new List<string>();
        foreach(ListItem itm in chlApplication.Items) {
            if(itm.Selected) {
                selectedApps.Add(itm.Value);
            }
        }
        try {
            IEnumerable<ApplicationSummary> summaries = ResourceBlenderDAL.GetResourceCompletionSummary(selectedApps, chkIncludeNotInUse.Checked);
            foreach(ApplicationSummary summary in summaries) {
                PlaceHolder plcSummary = new PlaceHolder();
                plcDynamic.Controls.Add(plcSummary);
                plcSummary.Controls.Add(new LiteralControl("<h2>" + string.Format(Resources.UIStrings.ResourceSummaryForName, summary.DisplayName) + "</h2>"));
                plcSummary.Controls.Add(new LiteralControl("<h3>" + Resources.UIStrings.Bundles + "</h3>"));
                BulletedList lstBundles = new BulletedList();
                plcSummary.Controls.Add(lstBundles);
                foreach(string bundle in summary.Bundles) {
                    lstBundles.Items.Add(bundle);
                }
                if(summary.Bundles.Count > 1) {
                    plcSummary.Controls.Add(new LiteralControl("<p>" + string.Format(Resources.UIStrings.NumElementsOverall, summary.TotalElements) + "</p>"));
                }
                StringBuilder graphBuffer = new StringBuilder();
                graphBuffer.Append("<h3>" + Resources.UIStrings.TranslationCompletion + "</h3>\n<div class=\"translations\">");
                foreach(LanguageCompletion language in summary.CompletionMap) {
                    int completionPercentage = (int)Math.Round((language.NumTranslations / (double)summary.TotalElements) * 100.0, 0);
                    if(completionPercentage > 100) {
                        completionPercentage = 100;
                    } else if(completionPercentage < 0) {
                        completionPercentage = 0;
                    }
                    graphBuffer.AppendLine("  <div class=\"row\">")
                               .AppendLine("    <div class=\"translationName\">")
                               .AppendFormat("      {1}<img height=\"11\" width=\"16\" src=\"{0}\" alt=\"{1}\" />\n", ResolveUrl("~/images/flags/" + language.Image + ".png"), language.Culture.EnglishName)
                               .AppendLine("    </div>\n    <div class=\"graph\">")
                               .AppendFormat("      <strong class=\"bar\" style=\"width:{0}%;\">{0}%</strong>\n", completionPercentage)
                               .AppendLine("    </div>")
                               .AppendLine("  </div>");
                }
                graphBuffer.AppendLine("</div>");
                Literal litGraph = new Literal();
                litGraph.Text = graphBuffer.ToString();
                plcSummary.Controls.Add(litGraph);
            }
        } catch(Exception ex) {
            lblError.Text = ex.Message;
        }
        mltMain.ActiveViewIndex = 2;
    }
}
